﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;


using Business;

namespace GestorEventos
{
    public partial class showEvento : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            int eventID;

            if (Request.QueryString["eventID"] != null)
            {
                eventID = Convert.ToInt32(Request.QueryString["eventID"].ToString());
                Evento business = new Evento();
                Evento evento = business.getEventoByID(eventID);
                int statusEvento = evento.getStatus();


                Label2Nome.Text = evento.getNome();
                Label2DataFim.Text = Convert.ToString(evento.getDataFim().ToShortDateString());
                Label2DataInicio.Text = Convert.ToString(evento.getDataInicio().ToShortDateString());
                Label2Preco.Text = Convert.ToString(evento.getPreco());
                Label2Descricao.Text = evento.getDescricao();
                Label2Categoria.Text = evento.getCategoria();

                if (Session["UserID"] != null)
                {
                    bool value;
                    int userID = Convert.ToInt32(Session["UserID"].ToString());


                    Presenca presenca = new Presenca(eventID, userID);

                    /*if (!Page.IsPostBack)
                    {
                        value = presenca.getPresencaUser(userID, eventID);
                        if (!value)
                        {
                            LabelNome.Text = "nao tem presenca";
                            ButtonPresenca.Text = "Eu vou!";
                        }
                        else
                        {
                            LabelNome.Text = "tem presenca";
                            ButtonPresenca.Text = "Eu não vou!";
                        }
                    }*/
                    if (!Page.IsPostBack)
                    {
                        value = presenca.getPresencaUser(userID, eventID);
                        if (!value)
                        {

                            ButtonPresenca.Text = "Eu vou!";
                            presenca.removePresenca();
                        }
                        else
                        {

                            ButtonPresenca.Text = "Eu não vou!";
                            presenca.insertPresenca();
                        }
                    }

                    if (Session["userStatus"] != null)
                    {
                        int status = Convert.ToInt32(Session["userStatus"].ToString());

                        // Utilizador
                        if (status == 1)
                        {
                            if (statusEvento == 2)
                            {
                                HyperLinkEditar.Text = "Editar Evento";
                                HyperLinkEditar.NavigateUrl = "editEvento.aspx?eventID=" + eventID + "";

                                LinkButtonDenunciar.Text = "Marcar para Revisão";
                            }
                            else {
                                Response.Redirect("Default.aspx");
                            }
                          
                        }

                        // Moderador
                        if (status > 1)
                        {
                            HyperLinkEditar.Text = "Editar Evento";
                            HyperLinkEditar.NavigateUrl = "editEvento.aspx?eventID=" + eventID + "";

                            if (statusEvento == 0)
                            {

                                LinkButtonRemover.Text = "Remover"; // set status 2

                            }
                            if (statusEvento == 1)
                            {

                                LinkButtonRemover.Text = "Remover"; // set status 2
                            }

                            if (statusEvento == 2)
                            {

                                LinkButtonDenunciar.Text = "Marcar para Revisão";

                                LinkButtonRemover.Text = "Remover"; // set status 0

                            }

                            if (statusEvento == 3)
                            {

                                LinkButtonDenunciar.Text = "Marcar como revisto"; // set status 2
                            
                                LinkButtonRemover.Text = "Remover"; // set status 0
                            }

                        }

                        // Administrador

                    }




                }

            }
            else {
                Response.Redirect("Default.aspx");
            
            }
        }

        public void comentarEvento(Object o, EventArgs e) {
            String value;
            
            int userID, eventID;
            String comment;
            DateTime dataActual;
            userID = Convert.ToInt32(Session["UserID"].ToString());
            
            eventID = Convert.ToInt32(Request.QueryString["eventID"].ToString());
            comment = TextBoxComentario.Text;
            dataActual = DateTime.Now;
            Label2Categoria.Text = dataActual.ToString();


            

            Comentario comentario = new Comentario(eventID, userID, "teste", comment, dataActual);
           value = comentario.insertComentario();

           Label2Nome.Text = value;
        
        }

        public void changePresenca(Object o, EventArgs e) {

            int eventID;
            int userID;
            bool value;
            bool balor;
            if(Session["UserID"] != null){
            
                userID = Convert.ToInt32(Session["UserID"].ToString());

                if(Request.QueryString["eventID"] != null){
                    eventID = Convert.ToInt32(Request.QueryString["eventID"].ToString());

                    Presenca p = new Presenca(eventID, userID);
                    value = p.getPresencaUser(userID, eventID);

                    if(value){
                        
                         balor = p.removePresenca();
                         Label2Preco.Text = "remove";
                         ButtonPresenca.Text = "Eu vou!";
                    }
                    if(!value){
                        
                         balor = p.insertPresenca();
                         Label2Preco.Text = "insert";
                         ButtonPresenca.Text = "Eu não vou!";

                    }

                }
            
            }
        
        }

        public void censurar(Object o, EventArgs e) {

            LinkButton b = (LinkButton)o;
            Label2Nome.Text = b.CommandArgument;
            //int commentID = Convert.ToInt32(c.CommandArgument.ToString());

            //Label2Nome.Text = Convert.ToString(commentID);

            //comment.setEstadoComentario(0);
            
        
        }


        public void setRemovido(Object o, EventArgs e)
        {
            bool value = false;
            int eventID;

            if (Request.QueryString["eventID"] != null)
            {
                eventID = Convert.ToInt32(Request.QueryString["eventID"].ToString());

                Evento ev = new Evento();
                Evento evento = new Evento (ev.getEventoByID(eventID));


                int statusEvento = evento.getStatus();

                if (statusEvento == 2) {
                    value = evento.setEstadoEvento(eventID, 0);
                    Response.Redirect("showEvento.aspx?eventID=" + eventID + "");
                }

                if (statusEvento == 0) {
                    value = evento.setEstadoEvento(eventID, 2);
                    Response.Redirect("showEvento.aspx?eventID=" + eventID + "");
                }

                if (statusEvento == 1)
                {
                    value = evento.setEstadoEvento(eventID, 2);
                    Response.Redirect("showEvento.aspx?eventID=" + eventID + "");
                }

 

            }

        }

        public void setDenunciado(Object o, EventArgs e)
        {
            bool value = false;
            int eventID;
            if (Request.QueryString["eventID"] != null)
            {
                eventID = Convert.ToInt32(Request.QueryString["eventID"].ToString());

                Evento ev = new Evento();
                Evento evento = new Evento(ev.getEventoByID(eventID));

                int statusEvento = evento.getStatus();

                if (statusEvento == 2) {
                    value = evento.setEstadoEvento(eventID,3);
                    Response.Redirect("showEvento.aspx?eventID=" + eventID + "");
                }

                if (statusEvento == 3) {
                    value = evento.setEstadoEvento(eventID,2);
                    Response.Redirect("showEvento.aspx?eventID=" + eventID + "");
                }


            }
            else
            {
                Response.Redirect("Default.aspx");

            }

        }

    }
}
